<template>
  <div id="Roles">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table border :data="RolesList">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="item in scope.row.children" :key="item.id">
              <el-col :span="5">
                <el-tag closable @close="removeItem(scope.row,item.id)">{{item.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="item1 in item.children" :key="item1.id">
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="removeItem(scope.row,item1.id)"
                      type="warning"
                    >{{item1.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      closable
                      @close="removeItem(scope.row,item2.id)"
                      type="danger"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                    >{{item2.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column width="300" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="提示" :visible.sync="SetDialogVisible" width="50%" @close="setRolesDialogClose">
      <el-tree
        ref="treeRef"
        :data="rightsList"
        :props="treeProps"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="defKeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="SetDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allSetRoles">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      RolesList: [],
      SetDialogVisible: false,
      rightsList: [],
      treeProps: {
        children: "children",
        label: "authName"
      },
      defKeys: [],
      roleId: ""
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get("roles");
      this.RolesList = res.data;
    },
    async removeItem(role, id) {
      const { data: res } = await this.$http.delete(
        `roles/${role.id}/rights/${id}`
      );

      role.children = res.data;
    },
    async showSetDialog(role) {
      const { data: res } = await this.$http.get("rights/tree");
      this.rightsList = res.data;
      this.getRolesKeys(role, this.defKeys);
      console.log(this.defKeys);
      this.SetDialogVisible = true;
      this.roleId = role.id;
    },

    getRolesKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach(item => {
        this.getRolesKeys(item, arr);
      });
    },
    setRolesDialogClose() {
      this.defKeys = [];
    },
    async allSetRoles() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ];
      const keyidstr = keys.join(",");
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: keyidstr }
      );
      this.getRolesList();
      this.SetDialogVisible = false;
    }
  }
};
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px 5px;
}
</style>